package com.test.io.buffer;

import org.junit.Test;

import java.io.*;

/**
 * @version 1.0
 * @Author: 许啸林  使用字节处理流复制文件
 * @Date: 2021/6/3 9:36
 */
public class BuffByteCopy {

    @Test
    public void copyFile() {
        String filePath = "d:\\bk.jpg";
        String targetPath = "d:\\bkcopy.png";
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(filePath));
            bos = new BufferedOutputStream(new FileOutputStream(targetPath));
            int readIndex = 0;
            byte[] readData = new byte[1024];
            while ((readIndex = bis.read(readData)) != -1) {
                bos.write(readData, 0, readIndex);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(bis != null) {
                    bis.close();
                }
                if(bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
